package com.travelbid.line.service;

import java.util.List;

import org.hibernate.Query;

import com.travelbid.line.dao.TCategoryDAO;
import com.travelbid.line.dao.TLineDAO;
import com.travelbid.line.dao.TSeatDAO;
import com.travelbid.line.pojo.TCategory;
import com.travelbid.line.pojo.TLine;
import com.travelbid.line.pojo.TSeat;
import com.travelbid.line.struts.form.AddLineForm;
import com.travelbid.line.struts.form.AddNewPlanForm;
import com.travelbid.line.struts.form.AddSeatForm;
import com.travelbid.line.struts.form.SelectLineForm;

public class LineService {

	private TCategoryDAO cateDAO;
	private TLineDAO lineDAO;
	private TSeatDAO seatDAO;
	
	public void addNewCategory(AddNewPlanForm planForm,Integer parId){
		TCategory cate = new TCategory();
		cate.setCategoryName(planForm.getCategoryName());
		if(planForm.getDoc()!=null){
			cate.setDoc(planForm.getDocPath());
		}
		cate.setPhoneNumber(planForm.getPhoneNumber());
		cate.setParentId(parId);
		cateDAO.save(cate);		
	}
	
	public void addNewLine(AddLineForm addLineForm){
		TLine line = new TLine();
		line.setLineName(addLineForm.getLineName());
		line.setParentId(addLineForm.getParId());
		line.setPrice(addLineForm.getPrice());
		line.setSerialNumber(addLineForm.getSerialNumber());
		if(addLineForm.getDoc()!=null){
			line.setDoc(addLineForm.getDocPath());
		}
		line.setContent(addLineForm.getContent());
		lineDAO.save(line);
	}
	
	public void addNewSeat(AddSeatForm addSeatForm){
		TSeat seat = new TSeat();
		seat.setParentId(addSeatForm.getParId());
		seat.setRemnant(addSeatForm.getRemnant());
		seat.setDate(addSeatForm.getDate());
		seatDAO.save(seat);
	}
	
	public TLine findLineById(Integer id){
		return lineDAO.findById(id);
	}
	
	
	public void editCategory(AddNewPlanForm planForm,Integer id){
		TCategory cate = cateDAO.findById(id);
		cate.setCategoryName(planForm.getCategoryName());
		if(planForm.getDoc()!=null){
			cate.setDoc(planForm.getDocPath());
		}
		cate.setPhoneNumber(planForm.getPhoneNumber());
		cateDAO.save(cate);	
	}	
	
	public void editLine(AddLineForm addLineForm,Integer id){
		TLine line = lineDAO.findById(id);
		line.setLineName(addLineForm.getLineName());
		line.setParentId(addLineForm.getParId());
		line.setPrice(addLineForm.getPrice());
		line.setSerialNumber(addLineForm.getSerialNumber());
		if(addLineForm.getDoc()!=null){
			line.setDoc(addLineForm.getDocPath());
		}
		line.setContent(addLineForm.getContent());
		lineDAO.save(line);
	}

	@SuppressWarnings("unchecked")
	public List<TLine> getAllLine(){
		return lineDAO.findAll();
	}
	
	
	@SuppressWarnings("unchecked")
	public List<TCategory> getAllPlan(){
		return cateDAO.findByParentId(0);
	}
	
	@SuppressWarnings("unchecked")
	public  List<TCategory> findAllSubordinate(){
		String hql = "from TCategory cate where cate.parentId in(select c.id from TCategory c where c.parentId = 0)";
		List<TCategory> list = cateDAO.getHibernateTemplate().find(hql);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<TCategory> getSubordinateByParId(Integer parId){
		return cateDAO.findByParentId(parId);
	}

	public void deleteCategory(String[] delId){
		for(String id : delId){
			TCategory cate = cateDAO.findById(new Integer(id));
			if(cate.getParentId().equals(0)){
				for(TCategory c : (List<TCategory>)cateDAO.findByParentId(cate.getCategoryId())){
					deleteSubCategory(c);
				}
				cateDAO.delete(cate);
			}else{
				deleteSubCategory(cate);
			}
		}
	}
	
	public void deleteSubCategory(TCategory cate){
		for(TLine line : (List<TLine>)lineDAO.findByParentId(cate.getCategoryId())){
			deleteLine(line);
		}
		cateDAO.delete(cate);
	}
	
	public void deleteLine(String[] delId){
		for(String id : delId){
			TLine line = lineDAO.findById(new Integer(id));
			deleteLine(line);
		}
	}
	
	public void deleteLine(TLine line){
		for(TSeat seat : (List<TSeat>)seatDAO.findByParentId(line.getLineId())){
			seatDAO.delete(seat);
		}
		lineDAO.delete(line);
	}
	
	public void deleteSeat(String[] delId){
		for(String id : delId){
			seatDAO.delete(seatDAO.findById(new Integer(id)));
		}
	}
	
	public Integer getLineListCount(SelectLineForm selectLineForm){
		if(selectLineForm!=null){
			String hql = "";
			if(selectLineForm.getKeyword()==null||"".equals(selectLineForm.getKeyword().trim())){
				selectLineForm.setKeyword("");
			}
			if(selectLineForm.getParId()!=null&&!selectLineForm.getParId().equals(-1)){
				hql="from TLine line where (line.lineName like '%"+selectLineForm.getKeyword()+"%' or line.serialNumber like '%"+selectLineForm.getKeyword()+"%') and line.parentId = " + selectLineForm.getParId();
			}else{
				hql="from TLine line where line.lineName like '%"+selectLineForm.getKeyword()+"%' or line.serialNumber like '%"+selectLineForm.getKeyword()+"%'";
			}
			Query query = lineDAO.getSessionFactory().openSession().createQuery(hql);
			return query.list().size();
		}else{
			Query query = lineDAO.getSessionFactory().openSession().createQuery("from TLine");
			return query.list().size();
		}
	}

	@SuppressWarnings("unchecked")
	public List<TLine> getLineList(SelectLineForm selectLineForm,Integer pageNum){
		if(selectLineForm!=null){
			String hql = "";
			if(selectLineForm.getKeyword()==null||"".equals(selectLineForm.getKeyword().trim())){
				selectLineForm.setKeyword("");
			}
			if(selectLineForm.getParId()!=null&&!selectLineForm.getParId().equals(-1)){
				hql="from TLine line where (line.lineName like '%"+selectLineForm.getKeyword()+"%' or line.serialNumber like '%"+selectLineForm.getKeyword()+"%') and line.parentId = " + selectLineForm.getParId();
			}else{
				hql="from TLine line where line.lineName like '%"+selectLineForm.getKeyword()+"%' or line.serialNumber like '%"+selectLineForm.getKeyword()+"%'";
			}
			Query query = lineDAO.getSessionFactory().openSession().createQuery(hql);
			query.setFirstResult((pageNum-1)*20); 
			query.setMaxResults(20); 
			return query.list();
		}else{
			Query query = lineDAO.getSessionFactory().openSession().createQuery("from TLine");
			query.setFirstResult((pageNum-1)*20); 
			query.setMaxResults(20); 
			return query.list();
		}
	}
	
	public List<TSeat> getSeatList(Integer parId,Integer pageNum){
		String hql = "";
		if(parId!=null&&parId.equals(-1)){
			hql="from TSeat seat order by seat.date desc";
			Query query = lineDAO.getSessionFactory().openSession().createQuery(hql);
			query.setFirstResult((pageNum-1)*20); 
			query.setMaxResults(20); 
			return query.list();
		}
		if(parId!=null&&!parId.equals(-1)){
			hql="from TSeat seat where seat.parentId = " + parId +"order by seat.date desc";
			Query query = lineDAO.getSessionFactory().openSession().createQuery(hql);
			query.setFirstResult((pageNum-1)*20); 
			query.setMaxResults(20); 
			return query.list();
		}
		return null;
	}
	public Integer getSeatListCount(Integer parId){
		String hql = "";
		if(parId!=null&&parId.equals(-1)){
			hql="select seat.id from TSeat seat order by seat.date desc";
			Query query = lineDAO.getSessionFactory().openSession().createQuery(hql);
			return query.list().size();
		}
		if(parId!=null&&!parId.equals(-1)){
			hql="select seat.id from TSeat seat where seat.parentId = " + parId +"order by seat.date desc";
			Query query = lineDAO.getSessionFactory().openSession().createQuery(hql);
			return query.list().size();
		}
		return 0;
	}
	
	public TCategory findCategoryById(Integer id){
		return cateDAO.findById(id);
	}
	
	public TCategoryDAO getCateDAO() {
		return cateDAO;
	}
	public void setCateDAO(TCategoryDAO cateDAO) {
		this.cateDAO = cateDAO;
	}
	public TLineDAO getLineDAO() {
		return lineDAO;
	}
	public void setLineDAO(TLineDAO lineDAO) {
		this.lineDAO = lineDAO;
	}
	public TSeatDAO getSeatDAO() {
		return seatDAO;
	}
	public void setSeatDAO(TSeatDAO seatDAO) {
		this.seatDAO = seatDAO;
	}
	
}
